package com.example.travelgd.controller;

import com.example.travelgd.entity.News;
import com.example.travelgd.service.NewsService;
import com.example.travelgd.util.FwResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/news")
@Tag(name = "NewsController", description = "景区动态管理")
public class NewsController {

    @Autowired
    private NewsService newsService;

    @GetMapping("/list")
    @Operation(summary = "获取动态列表")
    public FwResult<List<News>> getNewsList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize
    ) {
        try {
            List<News> newsList = newsService.getNewsList(page, pageSize);
            log.info("获取动态列表成功, 页码: {}, 数据条数: {}", page, newsList.size());
            return FwResult.ok(newsList, "获取动态列表成功");
        } catch (Exception e) {
            log.error("获取动态列表失败", e);
            return FwResult.failedMsg("获取动态列表失败");
        }
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取动态详情")
    public FwResult<News> getNewsDetail(
            @PathVariable Long id,
            @RequestParam(required = false, defaultValue = "unknown") String userId
    ) {
        try {
            News news = newsService.getNewsDetail(id, userId);
            if (news == null) {
                return FwResult.failedMsg("动态不存在");
            }
            log.info("获取动态详情成功, id: {}, userId: {}", id, userId);
            return FwResult.ok(news, "获取动态详情成功");
        } catch (Exception e) {
            log.error("获取动态详情失败, id: {}, userId: {}", id, userId, e);
            return FwResult.failedMsg("获取动态详情失败");
        }
    }

    @PostMapping
    @Operation(summary = "添加动态")
    public FwResult<Void> addNews(@RequestBody News news) {
        try {
            if (newsService.addNews(news)) {
                log.info("添加动态成功, title: {}", news.getTitle());
                return FwResult.okMsg("添加动态成功");
            }
            return FwResult.failedMsg("添加动态失败");
        } catch (Exception e) {
            log.error("添加动态失败", e);
            return FwResult.failedMsg("添加动态失败");
        }
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新动态")
    public FwResult<Void> updateNews(@PathVariable Long id, @RequestBody News news) {
        try {
            news.setId(id);
            if (newsService.updateNews(news)) {
                log.info("更新动态成功, id: {}", id);
                return FwResult.okMsg("更新动态成功");
            }
            return FwResult.failedMsg("更新动态失败");
        } catch (Exception e) {
            log.error("更新动态失败, id: {}", id, e);
            return FwResult.failedMsg("更新动态失败");
        }
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除动态")
    public FwResult<Void> deleteNews(@PathVariable Long id) {
        try {
            if (newsService.deleteNews(id)) {
                log.info("删除动态成功, id: {}", id);
                return FwResult.okMsg("删除动态成功");
            }
            return FwResult.failedMsg("删除动态失败");
        } catch (Exception e) {
            log.error("删除动态失败, id: {}", id, e);
            return FwResult.failedMsg("删除动态失败");
        }
    }
}